from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

"""
==>序列化和反序列化
==>加密and解密
"""


def changepassword(openid):
    """
    明文加密成密文
    """
    obj = Serializer(settings.SECRET_KEY, 600)
    info = {'openid': openid}

    token = obj.dumps(info).decode()

    return token


def restorepassword(access_token):
    """
        密文解密成明文
    """
    obj = Serializer(settings.SECRET_KEY, 600)
    info = access_token
    try:
        data = obj.loads(info)
    except BadData:
        return None
    openid = data.get('openid')

    return openid
